package com.blockchain.front.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blockchain.framework.config.ConfigureProvider;
import com.blockchain.framework.http.session.Session;
import com.blockchain.framework.http.session.authentication.AuthenticationException;
import com.blockchain.framework.resource.ResourceProvider;
import com.blockchain.framework.service.ServiceSession;
import com.blockchain.front.service.common.UserManage;
import com.blockchain.util.EmailUtil;
import com.blockchain.util.StringHelper;
import com.blockchain.variables.defines.SystemVariable;

public class SendEmailCode extends AbstractFrontServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void processPost(final HttpServletRequest request, HttpServletResponse response,
			final ServiceSession serviceSession) throws Throwable {
		PrintWriter out = response.getWriter();
		ConfigureProvider configureProvider = getResourceProvider().getResource(ConfigureProvider.class);
		String phone = request.getParameter("phone");
		if (StringHelper.isEmpty(phone)) {
			UserManage usanage=serviceSession.getService(UserManage.class);
			phone = usanage.getUser().iphone;
		}
		String verifyType = request.getParameter("type");
		UserManage userManage = serviceSession.getService(UserManage.class);
		String senType = verifyType + phone;

		if (verifyType.equals("REGISTER")) {
			if (!StringHelper.isEmpty(phone)) {
				if (userManage.isPhone(phone)) {
					StringBuilder sb = new StringBuilder();
					sb.append("[{'num':00,'msg':'");
					sb.append("err_email_haven" + "'}]");
					out.write(sb.toString());
					return;
				}
			} else {
				StringBuilder sb = new StringBuilder();
				sb.append("[{'num':04,'msg':'");
				sb.append("err_email！" + "'}]");
				out.write(sb.toString());
				return;
			}
			
		} else {
			if (!StringHelper.isEmpty(phone)) {
				if (!userManage.isPhone(phone)) {
					StringBuilder sb = new StringBuilder();
					sb.append("[{'num':00,'msg':'");
					sb.append("邮箱不存在" + "'}]");
					out.write(sb.toString());
					return;
				}
			} else {
				StringBuilder sb = new StringBuilder();
				sb.append("[{'num':04,'msg':'");
				sb.append("邮箱错误！" + "'}]");
				out.write(sb.toString());
				return;
			}
		}
		
		Session session = serviceSession.getSession();
		String code = session.getVerifyCode(senType);
		userManage.updateSend(phone, code);
		String content = "";
		
		if ("XLBZC".equals(verifyType)) {
			String y= "Dear BTB user, your verification code is SMS_TB , please do not share the verification code with anyone.. <br><br> ";
			String s = y + configureProvider.getProperty(SystemVariable.SMS_TB);
			content = s.replaceAll("SMS_TB", code);
		} else if ("WJJYMM".equals(verifyType)) {
			String y= "Dear BTB user, your verification code is SMS_TX , please do not share the verification code with anyone.. <br><br> ";
			String s =  y + configureProvider.getProperty(SystemVariable.SMS_WJMM);
			content = s.replaceAll("SMS_TX", code);
		} else if ("CNYTX".equals(verifyType)) {
			String y= "Dear BTB user, your verification code is SMS_TX , please do not share the verification code with anyone.. <br><br> ";
			String s =  y + configureProvider.getProperty(SystemVariable.SMS_TX);
			content = s.replaceAll("SMS_TX", code);
		} else if ("RESET".equals(verifyType)||"WJJYMM".equals(verifyType)) {
			String y= "Dear BTB user, your verification code is SMS_WJMM , please do not share the verification code with anyone.. <br><br> ";
			String s =  y + configureProvider.getProperty(SystemVariable.SMS_WJMM);
			content = s.replaceAll("SMS_WJMM", code);
		} else if ("REGISTER".equals(verifyType)) {
			String y= "Dear BTB user, your verification code is SMS_ZC , please do not share the verification code with anyone.. <br><br> ";
			String s = y + configureProvider.getProperty(SystemVariable.SMS_ZC);
			content = s.replaceAll("SMS_ZC", code);
		} else {
			String y= "Dear BTB user, your verification code is SMS_ZC , please do not share the verification code with anyone.. <br><br> ";
			String s = y + configureProvider.getProperty(SystemVariable.SMS_ZC);
			content = s.replaceAll("SMS_ZC", code);
		}
		EmailUtil.sendHtmlMail(phone,EmailUtil.NIKENAME, content+"<br><br><br><br>", "系统错误", true);
		StringBuilder sb = new StringBuilder();
		sb.append("[{'num':02,'msg':'");
		sb.append("err_email_send" + "'}]");
		out.write(sb.toString());
	}

	@Override
	protected void onThrowable(HttpServletRequest request, HttpServletResponse response, Throwable throwable)
			throws ServletException, IOException {
		ResourceProvider resourceProvider = getResourceProvider();
		resourceProvider.log(throwable);
		PrintWriter out = response.getWriter();
		StringBuilder sb = new StringBuilder();
		sb.append("[{'num':03,'msg':'");
		sb.append(throwable.getMessage() + "'}]");
		out.write(sb.toString());
		if (throwable instanceof AuthenticationException) {
			sendRedirect(request, response, getController().getURI(request, Index.class));
		}

	}

}
